package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.HighLeverFaultDto;
import com.hualude.roadsign.dto.DTHighLeverFaultDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.model.HighLeverFaultEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by xu_jiaHui on 2018/3/24.
 */
public interface HighLeverFaultRepository extends CrudRepository<HighLeverFaultEntity, Long> {
    /**
     * @Description: 获取窨井datatable
     * @Author: pan-jianan
     * @Date: 2017/8/11 17:20
     */
    public Integer getHighLeverReportFaultCount4DT(Integer actorId, Integer userId, Integer orgId, String strSearch) throws Exception;

    public List<DTHighLeverFaultDto> getHighLeverReportFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception;


    /**
     * @Description: 窨井故障详情
     * @Author: pan-jianan
     * @Date: 2017/8/11 17:20
     */
    @Query(value = "select new com.hualude.roadsign.dto.HighLeverFaultDto(cf.highLeverCheck,cf.highLeverMainCheck,cf.highLeverFeedback," +
            "cf.highLeverMoney,cf.highLeverFaultLevel,cf.highLeverFaultPhoto,cf.highLeverFaultType,cf.highLeverFaultReason,cf.highLeverFaultCreatorId," +
            "cf.highLeverFaultTime) " +
            "from HighLeverFaultEntity cf where cf.highLeverFaultId=?1")
    public HighLeverFaultDto getHighLeverFaultInfo2Fault(Integer highLeverFaultId) throws Exception;

    /**
     * @Description: 更新故障
     * @Author: pan-jianan
     * @Date: 2017/8/11 17:20
     */
    @Modifying
    @Query(value = "UPDATE HighLeverFaultEntity  cf SET cf.highLeverFaultLevel = ?2,cf.highLeverFaultType = ?3,cf.highLeverFaultPhoto = ?4,cf.highLeverFaultReason = ?5,cf.highLeverMoney =?6 WHERE cf.highLeverFaultId =?1")
    public Integer updateHighLeverFault(Integer highLeverFaultId, Integer highLeverFaultLevel, Integer highLeverFaultType, String highLeverFaultPhoto, String highLeverFaultReason, Double highLeverMoney) throws Exception;

    /**
     * @Description: 信号机故障审核
     * @Author: jiang-weirong
     * @Date: 2017/7/22 18:18
     */
    public List<DTHighLeverFaultDto> getHighLeverFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    public Integer getHighLeverFaultCount4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    /**
     * @Description: 主管审核通过
     * @Author: jiang-weirong
     * @Date: 2017/7/22 18:18
     */
    @Modifying
    @Query(value = "UPDATE HighLeverFaultEntity  mf SET mf.highLeverMainCheck= ?1,mf.highLeverMainCheckerId=?4,mf.highLeverFeedback=?2,mf.highLeverMaintenanceStatus=1,mf.highLeverMainCheckTime=?5 WHERE mf.highLeverFaultId=?3")
    public Integer highLeverFaultAudit2Success(Integer isPass, String feedback, Integer machineFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 主管审核拒绝
     * @Author: jiang-weirong
     * @Date: 2017/7/22 18:19
     */
    @Modifying
    @Query(value = "UPDATE HighLeverFaultEntity  mf SET mf.highLeverMainCheck= ?1,mf.highLeverMainCheckerId=?4,mf.highLeverFeedback=?2,mf.highLeverMainCheckTime=?5 WHERE mf.highLeverFaultId=?3")
    public Integer highLeverFaultAudit2Fail(Integer isPass, String feedback, Integer machineFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 故障处理DT
     * @Author: jiang-weirong
     * @Date: 2017/7/22 18:53
     */
    public List<DTHighLeverFaultDto> getHighLeverFaultHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer machineMaintenanceStatus) throws Exception;

    public Integer getHighLeverFaultHandleCount4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer machineMaintenanceStatus) throws Exception;

    /**
     * @Description: 施工方案
     * @Author: jiang-weirong
     * @Date: 2017/7/15 13:42
     */
    @Modifying
    @Query(value = "UPDATE HighLeverFaultEntity  mf SET mf.highLeverMaintenanceContent = ?2 WHERE mf.highLeverFaultId =?1")
    public Integer addHighLeverMaintenanceContent(Integer machineFaultId, String machineMaintenanceContent) throws Exception;

    /**
     * @Description: 完成故障维修
     * @Author: jiang-weirong
     * @Date: 2017/7/15 13:43
     */
    @Modifying
    @Query(value = "UPDATE HighLeverFaultEntity  mf SET mf.highLeverMaintenancePhoto = ?1,mf.highLeverMaintainerId=?2,mf.highLeverMaintenanceTime=?3,mf.highLeverMaintenanceExplain=?4,mf.highLeverMaintenanceStatus=2 WHERE mf.highLeverFaultId =?5")
    public Integer finishHighLeverMaintenance(String machineMaintenancePhoto, Integer machineMaintainerId, String machineMaintenanceTime, String machineMaintenanceExplain, Integer machineFaultId);

    /**
     * @Description: 获取企业负责人审核情况
     * @Author: jiang-weirong
     * @Date: 2017/7/22 20:35
     */
    @Query(value = "select mf.highLeverCheck from HighLeverFaultEntity mf where mf.highLeverFaultId = ?1")
    public Integer getMachineCheck(Integer highLeverFaultId) throws Exception;

    /**
     * @Description: 企业负责人审核
     * @Author: jiang-weirong
     * @Date: 2017/7/22 20:36
     */
    @Modifying
    @Query(value = "UPDATE HighLeverFaultEntity  mf SET mf.highLeverCheck= ?1,mf.highLeverCheckerId=?4,mf.highLeverFeedback=?2,mf.highLeverCheckTime=?5 WHERE mf.highLeverFaultId=?3")
    public Integer highLeverFaultAudit(Integer isPass, String feedback, Integer machineFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 删除窨井故障
     * @Author: pan-jianan
     * @Date: 2017/8/11 17:20
     */
    @Modifying
    @Query(value = "delete from HighLeverFaultEntity cf where cf.highLeverFaultId=?1")
    public Integer deleteHighLeverFaultByIds(Integer highLeverFaultId) throws Exception;

    /**
     * @Description: 通过ID删除窨井
     * @author xu_jiaHui
     * @date 2017/8/8 20:47
     */
    @Modifying
    @Query(value = "delete from HighLeverFaultEntity  where highLeverId=?1")
    public Integer deleteHighLeverFault(Integer highLeverId) throws Exception;


    /**
     * @Description: 判断是否窨井可上报：1可以，0不行
     * @Author: pan-jianan
     * @Date: 2017/8/11 17:20
     */
    @Query(value = "select new com.hualude.roadsign.dto.HighLeverFaultDto(cf.highLeverCheck,cf.highLeverMainCheck,cf.highLeverMaintenanceStatus) " +
            "from HighLeverFaultEntity cf where cf.highLeverId=?1  ORDER BY cf.highLeverFaultTime DESC ")
    public List<HighLeverFaultDto> getHighLeverFaultIsReport(Integer highLeverId) throws Exception;


    /**
     * @Description: 获取信号机审核列表4App
     * @Author: pan-jianan
     * @Date: 2017/7/17 16:18
     */
    public List<HighLeverFaultDto> getHighLeverFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus,
                                                                  Integer count, Integer size) throws Exception;

    /**
     * @Description: app获取信号机故障维修列表
     * @Author: pan-jianan
     * @Date: 2017/7/20 14:22
     */
    public List<HighLeverFaultDto> getHighLeverFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size) throws Exception;

    /**
     * @Description: app 回执单
     * @Author: pan-jianan
     * @Date: 2017/7/26 15:07
     */
    @Query(value = "select new com.hualude.roadsign.dto.HighLeverFaultDto(sm.highLeverId,mf.highLeverFaultLevel, mf.highLeverFaultPhoto," +
            "mf.highLeverFaultType,mf.highLeverFaultReason,mf.highLeverMainCheckTime,sm.highLeverLongitude,sm.highLeverLatitude,sm.highLeverProvince," +
            "sm.highLeverCity, sm.highLeverArea, sm.highLeverRoadName, sm.highLeverStartRoad, sm.highLeverEndRoad, mf.highLeverMaintenancePhoto," +
            " mf.highLeverMaintenanceContent, mf.highLeverMaintenanceExplain,mf.highLeverMaintenanceTime,mf.highLeverFeedback)" +
            " from HighLeverEntity sm,HighLeverFaultEntity mf " +
            "WHERE sm.highLeverId=mf.highLeverId AND mf.highLeverFaultId = ?1")
    public HighLeverFaultDto getMachineReceiptInfo4App(Integer machineFaultId) throws Exception;

    /**
     * @Description: 窨井故障列表4APP
     * @Author: pan-jianan
     * @Date: 2017/8/11 15:38
     */
    public List<HighLeverFaultDto> getHighLeverFaultList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception;

    /**
     * @Description: 获取推送相关信息
     * @Author: jiang-weirong
     * @Date: 2017/9/11 10:56
     */
    @Query(value = "select new com.hualude.roadsign.dto.HighLeverFaultDto(highLeverId, highLeverOrgId, highLeverMainOrgId, highLeverFaultType) from HighLeverFaultEntity where highLeverFaultId = ?1")
    public HighLeverFaultDto getInfoByFaultId(Integer highLeverFaultId) throws Exception;

    @Modifying
    @Query(value = "UPDATE HighLeverFaultEntity  lf SET lf.highLeverCheck= ?1,lf.highLeverCheckerId=?4,lf.highLeverFeedback=?2,lf.highLeverCheckTime=?5,lf.highLeverMainCheck=?1,lf.highLeverMainCheckTime=?5,lf.highLeverMaintenanceStatus=1 WHERE lf.highLeverFaultId=?3")
    public Integer straightFaultAudit(Integer isPass, String feedback, Integer highLeverFaultId, Integer userId, String dateNowStr);

}
